package com.shop.api;

import com.shop.po.R;
import com.shop.user.po.Cart;
import com.shop.user.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author banxia
 * @since 2023-02-25
 */
@RestController
@RequestMapping("/cart")
@CrossOrigin
public class CartController {

    @Autowired
    private CartService cartService;

    /**
     * 加入到购物车中
     * @param id
     * @param imagePath
     */
    @PostMapping("/add")
    public R add(@RequestParam("id") Long id,
                 @RequestParam("imagePath") String imagePath){
        cartService.add(id,imagePath);
        return R.ok("成功加入到购物车");
    }

    /**
     * 添加数量
     * @param id
     * @param count
     */
    @PostMapping("/addCart/{id}/{count}")
    public R addCart(@PathVariable("id") Long id,
                     @PathVariable("count") int count){
        cartService.addCart(id,count);
        return R.ok("成功添加到购物车");
    }

    /**
     * 获取用户购物车的所有记录
     * @return
     */
    @GetMapping("/getCart")
    public R getCart(){
        List<Cart> cart = cartService.getCart();
        return  R.ok("成功获取用户购物车记录").data("cart",cart);
    }

    @PostMapping("/delete/{id}")
    public R deleteById(@PathVariable Long id){
        boolean b = cartService.removeById(id);
        if (!b) return R.error();
        return R.ok();
    }
}

